package com.huixue.cn.ztej.utils.framwork;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huixue.cn.ztej.bean.DomainBase;
import com.huixue.cn.ztej.bean.Results;

public class JacksonUtil {
	private ObjectMapper objectMapper = null;

	public JacksonUtil() {
		objectMapper = new ObjectMapper();
	}

	public String getJson(Object obj) {
		try {
			objectMapper.setSerializationInclusion(Include.NON_NULL);
			return objectMapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getResultsLogJson(Object obj) {
		try {
			if(obj instanceof Results){
				Results primitiveResults = (Results)obj;
				Results results = new Results();
				results.setError(primitiveResults.getError());
				results.setStatus(primitiveResults.getStatus());
				return objectMapper.writeValueAsString(results);
			}else{
				return objectMapper.writeValueAsString(obj);
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Object getObject(String json, Class<? extends DomainBase> clazz){
		try {
			return objectMapper.readValue(json, clazz);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
